﻿using System.Linq;
using System.Collections.Generic;

namespace N2.Workflow.Items
{
	using Definitions;
	using Details;

	[Definition("Workflow", "Workflow")]
	[WithEditableName("Name", 01), WithEditableTitle]
	public class Workflow :
		ItemContainer<StateDefinition>,
		IWorkflowDefinition
	{
		[EditableLink("Initial State", 05)]
		public StateDefinition InitialState {
			get {
				return this.GetDetail<StateDefinition>(
					"InitialState",
					item => this.GetChild("new") as StateDefinition);
			}
			set { this.SetDetail<StateDefinition>("InitialState", value); }
		}

		public override bool IsPage { get { return false; } }
		public override string TemplateUrl { get { return "~/Templates/UI/Parts/Empty.ascx"; } }

		public override string Title { get { return base.Title ?? this.Name; } }

		public override string IconUrl { get { return
			null == this.InitialState
				? Constants.IconPath + "workflow.png"
				: this.InitialState.IconUrl; } }

		public override bool Visible { get { return false; } }

		#region IWorkflow Members

		IStateDefinition IWorkflowDefinition.InitialState {
			get { return this.InitialState as IStateDefinition; }
		}

		IEnumerable<IStateDefinition> IWorkflowDefinition.States {
			get { return this.Items.Cast<IStateDefinition>(); }
		}

		#endregion
	}
}